/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.operation.utils;

import cn.icanci.basic.commonutils.LogUtil;
import cn.icanci.operation.client.LogParserClient;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.stereotype.Component;

/**
 * TODO 此处把工具类作为一个服务，不是很优雅
 *
 * @author icanci
 * @date Created in 2021/04/27 14:49
 * @version 1.0
 */
@SuppressWarnings("all")
@Component
public class TradeLogService {
    @Resource
    private LogParserClient logParserClient;

    public void setLogInfo(String module, String category, String subCategory) {
        setModule(module);
        setCategory(category);
        setSubCategory(subCategory);
    }

    public void setModule(String module) {
        LogUtil.setModule(module);
    }

    public void setCategory(String category) {
        LogUtil.setCategory(category);
    }

    public void setSubCategory(String subCategory) {
        LogUtil.setSubCategory(subCategory);
    }

    public String getModule() {
        return LogUtil.getModule();
    }

    public String getCategory() {
        return LogUtil.getCategory();
    }

    public String getSubCategory() {
        return LogUtil.getSubCategory();
    }

    public void removeAll() {
        LogUtil.removeAll();
    }

    public void info(Logger logger, String format, Object... obj) {
        FormattingTuple ft = MessageFormatter.arrayFormat(format, obj);
        logParserClient.sendLogMessage(fixMessage(ft.getMessage(), Integer.valueOf("1")));
        LogUtil.info(logger, format, obj);
    }

    public void warn(Logger logger, String format, Object... obj) {
        FormattingTuple ft = MessageFormatter.arrayFormat(format, obj);
        logParserClient.sendLogMessage(fixMessage(ft.getMessage(), Integer.valueOf("2")));
        LogUtil.warn(logger, format, obj);
    }

    public void error(Logger logger, String format, Object... obj) {
        FormattingTuple ft = MessageFormatter.arrayFormat(format, obj);
        logParserClient.sendLogMessage(fixMessage(ft.getMessage(), Integer.valueOf("3")));
        LogUtil.error(logger, format, obj);
    }

    /**
     * 封装日志数据
     * @param message
     * @return
     */
    private String fixMessage(String message, Integer index) {
        StringBuilder builder = new StringBuilder();
        return builder.append(getModule()).append("^")//
            .append(getCategory()).append("^")//
            .append(getSubCategory()).append("^")//
            .append(message).append("^")//
            .append(index).toString();
    }
}
